home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdAECSSAttributes.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  5.0 KB  |  176 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Ben "Count XULula" Goodger
  22.  *   Daniel Glazman <glazman@netscape.com>
  23.  */
  24.  
  25. // build attribute list in tree form from element attributes
  26. function BuildCSSAttributeTable()
  27. {
  28.   // we can't trust DOM 2 ElementCSSInlineStyle because gElement can be
  29.   // outside of the document's tree
  30.   var styleAttr = gElement.getAttribute("style");
  31.   var editor = editorShell.editor.QueryInterface(Components.interfaces.nsIHTMLEditor);
  32.   var styleRule = editor.parseStyleAttrIntoCSSRule(styleAttr);
  33.   
  34.   if (styleRule == undefined)
  35.   {
  36.     dump("Inline styles undefined\n");
  37.     return;
  38.   }
  39.  
  40.   var style = styleRule.style;
  41.   var declLength = style.length;
  42.  
  43.   if (declLength == undefined || declLength == 0)
  44.   {
  45.     if (declLength == undefined) {
  46.       dump("Failed to query the number of inline style declarations\n");
  47.     }
  48.  
  49.     return;
  50.   }
  51.  
  52.   if (declLength > 0)
  53.   {
  54.     var added = false;
  55.     for (i = 0; i < declLength; i++)
  56.     {
  57.       name = style.item(i);
  58.       value = style.getPropertyValue(name);
  59.       AddTreeItem( name, value, "CSSAList", CSSAttrs );
  60.     }
  61.   }
  62.  
  63.   ClearCSSInputWidgets();
  64. }
  65.  
  66. function onChangeCSSAttribute()
  67. {
  68.   var name = TrimString(gDialog.AddCSSAttributeNameInput.value);
  69.   if ( !name )
  70.     return;
  71.  
  72.   var value = TrimString(gDialog.AddCSSAttributeValueInput.value);
  73.  
  74.   // First try to update existing attribute
  75.   // If not found, add new attribute
  76.   if ( !UpdateExistingAttribute( name, value, "CSSAList" ) && value)
  77.     AddTreeItem( name, value, "CSSAList", CSSAttrs );
  78. }
  79.  
  80. function ClearCSSInputWidgets()
  81. {
  82.   gDialog.AddCSSAttributeTree.treeBoxObject.selection.clearSelection();
  83.   gDialog.AddCSSAttributeNameInput.value ="";
  84.   gDialog.AddCSSAttributeValueInput.value = "";
  85.   SetTextboxFocus(gDialog.AddCSSAttributeNameInput);
  86. }
  87.  
  88. function onSelectCSSTreeItem()
  89. {
  90.   if (!gDoOnSelectTree)
  91.     return;
  92.  
  93.   var tree = gDialog.AddCSSAttributeTree;
  94.   if (tree && tree.treeBoxObject.selection.count)
  95.   {
  96.     gDialog.AddCSSAttributeNameInput.value = GetTreeItemAttributeStr(getSelectedItem(tree));
  97.     gDialog.AddCSSAttributeValueInput.value = GetTreeItemValueStr(getSelectedItem(tree));
  98.   }
  99. }
  100.  
  101. function onInputCSSAttributeName()
  102. {
  103.   var attName = TrimString(gDialog.AddCSSAttributeNameInput.value).toLowerCase();
  104.   var newValue = "";
  105.  
  106.   var existingValue = GetAndSelectExistingAttributeValue(attName, "CSSAList");
  107.   if (existingValue)
  108.     newValue = existingValue;
  109.  
  110.   gDialog.AddCSSAttributeValueInput.value = newValue;
  111. }
  112.  
  113. function editCSSAttributeValue(targetCell)
  114. {
  115.   if (IsNotTreeHeader(targetCell))
  116.     gDialog.AddCSSAttributeValueInput.inputField.select();
  117. }
  118.  
  119. function UpdateCSSAttributes()
  120. {
  121.   var CSSAList = document.getElementById("CSSAList");
  122.   var styleString = "";
  123.   for(var i = 0; i < CSSAList.childNodes.length; i++)
  124.   {
  125.     var item = CSSAList.childNodes[i];
  126.     var name = GetTreeItemAttributeStr(item);
  127.     var value = GetTreeItemValueStr(item);
  128.     // this code allows users to be sloppy in typing in values, and enter
  129.     // things like "foo: " and "bar;". This will trim off everything after the
  130.     // respective character.
  131.     if (name.indexOf(":") != -1)
  132.       name = name.substring(0,name.lastIndexOf(":"));
  133.     if (value.indexOf(";") != -1)
  134.       value = value.substring(0,value.lastIndexOf(";"));
  135.     if (i == (CSSAList.childNodes.length - 1))
  136.       styleString += name + ": " + value + ";";   // last property
  137.     else
  138.       styleString += name + ": " + value + "; ";
  139.   }
  140.   if (styleString)
  141.   {
  142.     // Use editor transactions if modifying the element directly in the document
  143.     doRemoveAttribute("style");
  144.     doSetAttribute("style", styleString);  // NOTE BUG 18894!!!
  145.   } 
  146.   else if (gElement.getAttribute("style"))
  147.     doRemoveAttribute("style");
  148. }
  149.  
  150. function RemoveCSSAttribute()
  151. {
  152.   var treechildren = gDialog.AddCSSAttributeTree.lastChild;
  153.  
  154.   // We only allow 1 selected item
  155.   if (gDialog.AddCSSAttributeTree.treeBoxObject.selection.count)
  156.   {
  157.     var item = getSelectedItem(gDialog.AddCSSAttributeTree);
  158.  
  159.     // Remove the item from the tree
  160.     // We always rebuild complete "style" string,
  161.     //  so no list of "removed" items 
  162.     treechildren.removeChild (item);
  163.  
  164.     ClearCSSInputWidgets();
  165.   }
  166. }
  167.  
  168. function SelectCSSTree( index )
  169. {
  170.   gDoOnSelectTree = false;
  171.   try {
  172.     gDialog.AddCSSAttributeTree.selectedIndex = index;
  173.   } catch (e) {}
  174.   gDoOnSelectTree = true;
  175. }
  176.